<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" >

<title>django打包下载后删除文件 | janl&#39;s workspace</title>

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="shortcut icon" href="https://janl.gitee.io/favicon.ico?v=1658806429692">
<link rel="stylesheet" href="https://janl.gitee.io/styles/main.css">



<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>



    <meta name="description" content="django视图API需要将指定文件打包为download.zip后下载，待下载完成后将打包文件删除。

定义待下载文件包装器类
class FileDeleteWrapper(FileWrapper):
    &quot;&quot;&..." />
    <meta name="keywords" content="python,django,软件开发" />
  </head>
  <body>
    <div id="app" class="main">

      <div class="sidebar" :class="{ 'full-height': menuVisible }">
  <div class="top-container" data-aos="fade-right">
    <div class="top-header-container">
      <a class="site-title-container" href="https://janl.gitee.io">
        <img src="https://janl.gitee.io/images/avatar.png?v=1658806429692" class="site-logo">
        <h1 class="site-title">janl&#39;s workspace</h1>
      </a>
      <div class="menu-btn" @click="menuVisible = !menuVisible">
        <div class="line"></div>
      </div>
    </div>
    <div>
      
        
          <a href="/" class="site-nav">
            首页
          </a>
        
      
        
          <a href="/archives" class="site-nav">
            归档
          </a>
        
      
        
          <a href="/tags" class="site-nav">
            标签
          </a>
        
      
        
          <a href="/post/about" class="site-nav">
            关于
          </a>
        
      
    </div>
  </div>
  <div class="bottom-container" data-aos="flip-up" data-aos-offset="0">
    <div class="social-container">
      
        
      
        
      
        
      
        
      
        
      
    </div>
    <div class="site-description">
      道可道 非常道
    </div>
    <div class="site-footer">
      Powered by <a href="https://janl.gitee.io" target="_blank">Janl</a> | <a class="rss" href="https://janl.gitee.io/atom.xml" target="_blank">RSS</a>
    </div>
  </div>
</div>


      <div class="main-container">
        <div class="content-container" data-aos="fade-up">
          <div class="post-detail">
            <h2 class="post-title">django打包下载后删除文件</h2>
            <div class="post-date">2022-07-21</div>
            
              <div class="feature-container" style="background-image: url('https://janl.gitee.io/post-images/django-da-bao-xia-zai-hou-shan-chu-wen-jian.png')">
              </div>
            
            <div class="post-content" v-pre>
              <p>django视图API需要将指定文件打包为download.zip后下载，待下载完成后将打包文件删除。</p>
<!-- more -->
<p>定义待下载文件包装器类</p>
<pre><code>class FileDeleteWrapper(FileWrapper):
    &quot;&quot;&quot;
    文件打包下载包装器，打包下载后删除打包文件
    &quot;&quot;&quot;
    def __init__(self, filepath, *args, **kwargs):
        self.filepath = filepath
        print(&quot;__init__ FileDeleteWrapper filepath:&quot;, self.filepath)
        super(FileDeleteWrapper, self).__init__(*args, **kwargs)

    def __del__(self, *args, **kwargs):
        print(&quot;__del__ FileDeleteWrapper filepath:&quot;, self.filepath)
        os.remove(self.filepath)
</code></pre>
<p>在django视图代码中作如下调用</p>
<pre><code>zip_path = root_path + '/' + file_path
chunk_size = 16384
del_file = FileDeleteWrapper(filepath=zip_path, filelike=open(zip_path, 'rb'),  blksize=chunk_size)
response = StreamingHttpResponse(del_file)
response['Content-Length'] = os.path.getsize(zip_path)
response['Content-Type'] = 'application/octet-stream'
cd = 'attachment;filename=' + file_path
response['Content-Disposition'] = cd.encode(&quot;utf-8&quot;).decode(&quot;latin1&quot;)
return response
</code></pre>

            </div>
            
              <div class="tag-container">
                
                  <a href="https://janl.gitee.io/tag/upxStUD1Y/" class="tag">
                    python
                  </a>
                
                  <a href="https://janl.gitee.io/tag/3HBpraHKd/" class="tag">
                    django
                  </a>
                
                  <a href="https://janl.gitee.io/tag/ZDuG-9Yvk/" class="tag">
                    软件开发
                  </a>
                
              </div>
            
            
              <div class="next-post">
                <div class="next">下一篇</div>
                <a href="https://janl.gitee.io/post/mysql-chuang-jian-shu-ju-ku-bing-zhi-ding-utf8-zi-fu-ji/">
                  <h3 class="post-title">
                    Mysql创建数据库并指定utf8字符集
                  </h3>
                </a>
              </div>
            

            

          </div>

        </div>
      </div>
    </div>

    <script src="https://unpkg.com/aos@next/dist/aos.js"></script>
<script type="application/javascript">

AOS.init();

var app = new Vue({
  el: '#app',
  data: {
    menuVisible: false,
  },
})

</script>


  <script src="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.5.1/build/highlight.min.js"></script>
  <script>
    hljs.initHighlightingOnLoad()
  </script>





  </body>
</html>
